//
//  EDSCustomLeftRightBtnView.m
//  DrivieSchoolStudents
//
//  Created by TangBin on 2019/6/19.
//  Copyright © 2019 班文政. All rights reserved.
//

#import "EDSCustomLeftRightBtnView.h"

@interface EDSCustomLeftRightBtnView ()
@property (nonatomic, strong) UIButton *btnLeft;
@property (nonatomic, strong) UIButton *btnRight;

@property (nonatomic, copy) NSString *leftTitle;
@property (nonatomic, strong) UIColor *leftColor;
@property (nonatomic, copy) NSString *rightTitle;
@property (nonatomic, strong) UIColor *rightColor;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) CALayer *bottomLine;

- (void)initWithSubView;

- (void)initWithBtnLeft;
- (void)initWithBtnRight;

@end

@implementation EDSCustomLeftRightBtnView

/**
 初始化
 
 @param frame frame description
 @param leftTitle 左边按钮的标题
 @param leftColor 左边按钮的颜色
 @param rightTitle 右边按钮的标题
 @param rightColor 右边按钮的颜色
 @return return value description
 */
- (instancetype)initWithFrame:(CGRect)frame leftTitle:(NSString *)leftTitle leftColor:(UIColor *)leftColor rightTitle:(NSString *)rightTitle rightColor:(UIColor *)rightColor
{
    if (self = [super initWithFrame:frame])
    {
        self.leftTitle = leftTitle;
        self.leftColor = leftColor;
        self.rightTitle = rightTitle;
        self.rightColor = rightColor;
        [self initWithSubView];
    }
    return self;
}

- (void)initWithSubView
{
    self.backgroundColor = [UIColor colorWithHexString:@"#1883c7"];
    [self initWithBtnLeft];
    [self initWithBtnRight];
    
    [self initWithTitleLabel];
    
    CGRect frame = CGRectMake(0, self.height - 1, self.width, 1);
    _bottomLine = [CALayer drawLine:self frame:frame lineColor:[UIColor clearColor]];
}

- (void)initWithBtnLeft
{
    UIFont *font = [UIFont systemFontOfSize:16];
    float width = [self.leftTitle widthForFont:font] + 2;
    CGRect frame = CGRectMake(20, (kEDSCustomLeftRightBtnViewHeight - 30) * 0.5, width, 30);
    _btnLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnLeft.frame = frame;
    _btnLeft.tag = EN_BUTTON_TYPE_CANCEL_TAG;
    _btnLeft.titleLabel.font = font;
    [_btnLeft setTitle:self.leftTitle forState:UIControlStateNormal];
    [_btnLeft setTitleColor:self.leftColor forState:UIControlStateNormal];
    [_btnLeft addTarget:self action:@selector(clickedBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnLeft];
}
- (void)initWithBtnRight
{
    UIFont *font = [UIFont systemFontOfSize:16];
    CGRect frame = _btnLeft.frame;
    frame.origin.x = self.width - 20 - frame.size.width;
    _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
    _btnRight.frame = frame;
    _btnRight.tag = EN_BUTTON_TYPE_SUBMIT_TAG;
    _btnRight.titleLabel.font = font;
    [_btnRight setTitle:self.rightTitle forState:UIControlStateNormal];
    [_btnRight setTitleColor:self.rightColor forState:UIControlStateNormal];
    [_btnRight addTarget:self action:@selector(clickedBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_btnRight];
}

- (void)initWithTitleLabel
{
    CGRect frame = CGRectMake(_btnLeft.right + 5, 0, self.btnRight.left - _btnLeft.right - 10, self.height);
    _titleLabel = [UILabel createWithLabel:self frame:frame font:FONTSIZE(15) propertiesBlock:^(UILabel * _Nonnull label) {
        label.text = @"请选择时间";
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
    }];
}

- (void)setButtonTextColor:(UIColor *)buttonTextColor
{
    _buttonTextColor = buttonTextColor;
    _leftColor = buttonTextColor;
    _rightColor = buttonTextColor;
    [_btnLeft setTitleColor:_leftColor forState:UIControlStateNormal];
    [_btnRight setTitleColor:_rightColor forState:UIControlStateNormal];
}

- (void)setBottomLineColor:(UIColor *)bottomLineColor
{
    _bottomLineColor = bottomLineColor;
    _bottomLine.backgroundColor = bottomLineColor.CGColor;
}

- (void)clickedBtn:(id)sender
{
    UIButton *btn = (UIButton *)sender;
    if (self.touchButtonBlock)
    {
        self.touchButtonBlock(btn.tag);
    }
}

@end
